package com.jn.jetpack.views;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.jn.jetpack.R;
import com.jn.jetpack.base.BaseActivity;
import com.jn.jetpack.contract.QAndAContract;
import com.jn.jetpack.dao.QAndADao;
import com.jn.jetpack.entity.QAndAEntity;
import com.jn.jetpack.presenter.QAndAPresenter;

import javax.inject.Inject;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint // 允许 Hilt 注入依赖
public class QAndAActivity extends BaseActivity
        implements QAndAContract.IQAndAView, View.OnClickListener {
    private QAndAContract.IQAndAPresenter mPresenter;
    private Button mButton;
    private Button mButton1;
    private TextView mQuestionText;
    private TextView mAnswerText;
    @Inject
    QAndADao qAndADao;
    @Override
    public int getLayoutId() {
        return R.layout.activity_qanda;
    }

    @Override
    public void init() {
        mPresenter = new QAndAPresenter(this, this);
        mButton = findViewById(R.id.button);
        mButton1 = findViewById(R.id.to_list);
        mQuestionText = findViewById(R.id.question);
        mAnswerText = findViewById(R.id.answer);
        mButton.setOnClickListener(this);
        mButton1.setOnClickListener(this);
    }

    @Override
    public void showData(QAndAEntity data) {
        if (data != null) {
            qAndADao.insert(data);
            mQuestionText.setText(data.getQuestion());
            mAnswerText.setText(data.getAnswer());
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.button) {
            if (mPresenter != null) {
                mPresenter.getData();
            }
        } else if (id == R.id.to_list) {
            Intent intent = new Intent(QAndAActivity.this, QAndAListActivity.class);
            startActivity(intent);
        }
    }

    @Override
    public void onHttpLoading(boolean loading) {
        if (loading) {
            showLoading();
        } else {
            dismissLoading();
        }
    }

    @Override
    public void onHttpFail(String... msg) {
        if (msg != null && msg.length > 0) {
            showToast(msg[0]);
        }
    }

    @Override
    public void onHttpTimeout() {

    }

    @Override
    public void onHttpTokenExpiration() {

    }
}